var express = require('express')
var router = express.Router()
const sqlQuery = require('../../../module/sqlQuery')
const { resJsonObject } = require('../../../module/canshu')

// 分类管理==================================================

/* GET users listing. */
router.get('/', function (req, res, next) {
  res.render('admin/doctors/category/categoryList')
})

router.get('/data', async function (req, res, next) {
  try {
    // keywords 搜索关键词 为空的时候 查询全部
    let { page, limit } = req.query
    console.log(req.query)

    page = parseInt(page ? page : 1)
    limit = parseInt(limit)
    const results = await sqlQuery('select * from doctors_category limit ?,?', [
      (page - 1) * 5,
      limit,
    ])

    // 符合条件的总条数
    const [{ count_num }] = await sqlQuery(
      'select count(*) as count_num from doctors_category'
    )

    console.log(results)

    res.json(
      resJsonObject({
        code: 0,
        msg: '完成',
        data: {
          arr: Array.from(results),
          count: count_num,
        },
      })
    )
    // res.render('admin/users/userlist',{userlist:results});
  } catch (error) {
    res.json(
      resJsonObject({
        code: 1,
        msg: '获取失败',
        data: {
          error,
        },
      })
    )
  }
})

// 信息界面 添加
router.get('/add', async function (req, res, next) {
  // 获取角色列表
  // const roleList = await getRoleList()
  res.render('admin/doctors/category/addCategory', {})
})

router.post('/add', async function (req, res, next) {
  try {
    console.log(req.body)
    const { category_name, category_brief } = req.body

    const sqlStr = `insert into doctors_category (name,brief) values (?,?)`
    await sqlQuery(sqlStr, [category_name, category_brief])
    // console.log(results)
    res.json(
      resJsonObject({
        code: 0,
        msg: '添加成功',
      })
    )
  } catch (error) {
    console.log(error)
    res.json(
      resJsonObject({
        code: 1,
        msg: '添加失败',
        data: {
          error,
        },
      })
    )
  }
})

// 用户信息删除（update状态改位为 0 ）
router.post('/delete', async function (req, res, next) {
  const { id } = req.body
  const results = await sqlQuery('delete from doctors_category where id=?', [
    id,
  ])
  res.json(
    resJsonObject({
      code: 0,
      msg: '删除成功',
      data: {
        results,
      },
    })
  )
})

// 信息界面 编辑
router.get('/edit/:id', async function (req, res, next) {
  let sqlStr = `select * from doctors_category where id=?`

  // 获取角色列表
  const results = await sqlQuery(sqlStr, [req.params.id])
  // res.json(results)
  const options = { categoryinfo: results[0] }
  res.render('admin/doctors/category/editCategory.ejs', options)
})

router.post('/edit', async function (req, res, next) {
  try {
    let { category_name, category_brief, category_id } = req.body

    // 1，更新表，
    const resultForRoleListTable = await sqlQuery(
      'update doctors_category set name =?,  brief =? where id=?',
      [category_name, category_brief, parseInt(category_id)]
    )

    res.json(
      resJsonObject({
        code: 0,
        msg: '更新成功',
      })
    )
  } catch (error) {
    console.log(error)
    res.json(
      resJsonObject({
        code: 1,
        msg: '更新失败',
        data: {
          error,
        },
      })
    )
  }
})
module.exports = router
